﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.ServiceModel.Web;
using System.Threading;
using System.ServiceModel.Description;

namespace MulticastService
{
    // 注意: 如果更改此处的类名“Service1”，也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
    public class Service : IService
    {
        ///////////////////////////////
        //测试管理接口
        public static string s;

        public string GetServerState()
        {
            return "正在调试";
        }

        public void cs()
        {
            using (WebChannelFactory<ClusteringService.IService> wcf = new WebChannelFactory<ClusteringService.IService>(new Uri("http://localhost/ClusteringService/Service.svc")))
            {
                ClusteringService.IService channel = wcf.CreateChannel();
                s = channel.Hello();
            }
        }

        public string GetClusteringServerState()
        {
            string s; 
            //var webBinding = new WebHttpBinding();
            var channel = new WebChannelFactory<ClusteringService.IService>(new Uri("http://localhost/ClusteringService/Service.svc"));
            channel.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
            var proxy = channel.CreateChannel(); 
            using(new OperationContextScope((IContextChannel) proxy))
            {
                s=proxy.GetServerState();
            }
            return s;
        }

        ///////////////////////////////
        //组播客户端接口
        public ConfirmMessage Login(string clustering_key)
        {
            ClusteringService.PeerClusterMessage m = new  ClusteringService.PeerClusterMessage();
            var channel = new WebChannelFactory<ClusteringService.IService>(new Uri("http://localhost/ClusteringService/Service.svc"));
            channel.Endpoint.Behaviors.Add(new WebHttpBehavior());
            var proxy = channel.CreateChannel();
            using (new OperationContextScope((IContextChannel)proxy))
            {
                m = proxy.QueryPeer("default-application-key", clustering_key);
            }
            //Thread.Sleep(200);
            if (m.confirm.call_confirmed == true)
            {
                return ConfirmMessage.ConfirmedMessage();
            }
            else
                return ConfirmMessage.UnconfirmedMessage("invalid clustering key");
        }

        ///////////////////////////////
        //数据流服务器接口
    }
}
